﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('DbServer:page')">
        <a-form layout="inline">
          <a-row :gutter="48"
            ><a-col :md="8" :sm="24">
              <a-form-item label="数据库类型">
                <a-select
                  :allowClear="true"
                  style="width: 100%"
                  v-model="queryParam.dbType"
                  placeholder="请选择数据库类型"
                >
                  <a-select-option v-for="(item, index) in dbTypeData" :key="index" :value="item.code">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="服务器IP或主机名">
                <a-input v-model="queryParam.host" allow-clear placeholder="请输入服务器IP或主机名" />
              </a-form-item> </a-col
            >

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('DbServer:add')">
          <a-button type="primary" v-if="hasPerm('DbServer:add')" icon="plus" @click="$refs.addForm.add()"
            >新增数据库服务</a-button
          >
        </template>
        <span slot="dbtitle" slot-scope="text, record">
          <a-icon type="bulb" v-if="record.isAgentAlived" :style="{ color: 'green' }"/>
          <a-icon type="close-circle" v-if="!record.isEnabled" theme="twoTone" two-tone-color="red" />
          {{ text }}
        </span>
        <span slot="dbTypescopedSlots" slot-scope="text">
          {{ 'DbType' | enumType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('DbServer:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('DbServer:edit') & hasPerm('DbServer:delete')" />
          <a-popconfirm
            v-if="hasPerm('DbServer:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => DbServerDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import { DbServerPage, DbServerDelete } from '@/api/modular/main/DbServerManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
  },
  data() {
    return {
      queryParam: {},
      columns: [
        {
          title: '标题',
          align: 'left',
          dataIndex: 'title',
          scopedSlots:{customRender:'dbtitle'}
        },
        {
          title: '数据库类型',
          align: 'center',
          sorter: true,
          dataIndex: 'dbType',
          scopedSlots: { customRender: 'dbTypescopedSlots' },
        },
        {
          title: '数据库版本',
          align: 'center',
          dataIndex: 'dbServerVersion',
        },
        {
          title: '服务器IP或主机名',
          align: 'center',
          sorter: true,
          dataIndex: 'host',
        },
        {
          title: '数据库连接',
          align: 'center',
          dataIndex: 'options',
        },
        {
          title: '数据库服务名称',
          align: 'center',
          dataIndex: 'serverName',
        },
        {
          title: 'Agent身份ID',
          align: 'center',
          dataIndex: 'agentId',
        },
        {
          title: '通讯端口号',
          align: 'center',
          dataIndex: 'agentPort',
        },
        {
          title: 'Agent密钥',
          align: 'center',
          dataIndex: 'agentSecretKey',
        },
        {
          title: 'alive消息',
          align: 'center',
          dataIndex: 'aliveMessage',
        },
        {
          title: '最近在线时间',
          align: 'center',
          dataIndex: 'lastAliveTime',
          customRender: (val) => {
            return val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : ''
          },
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return DbServerPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      dbTypeData: [],
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
    if (this.hasPerm('DbServer:edit') || this.hasPerm('DbServer:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      })
    }
    const dbTypeOption = this.$options
    this.dbTypeData = dbTypeOption.filters['enumData']('DbType')
  },
  methods: {
    /**
     * 查询参数组装
     */
    switchingDate() {
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    DbServerDelete(record) {
      DbServerDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
